﻿Imports System.ComponentModel
Imports System.Collections.ObjectModel
Imports System.Threading.Tasks
Imports System.Threading
Imports System.Windows.Threading
Imports System.Xml

Public Class DBRepairWizardViewModel
    Implements INotifyPropertyChanged

    Private _dispatcher As Dispatcher
    Private _backupcol As ObservableCollection(Of BackUpModel)

    Public Sub New(dp As Dispatcher)
        _dispatcher = dp
        LoadBackups()
    End Sub

    Private Sub LoadBackups()
        Dim path As String = System.AppDomain.CurrentDomain.BaseDirectory() + "\backuplist.xml"
        If Not IO.File.Exists(path) Then
            Dim fn As New IO.StreamWriter(IO.File.Open(path, IO.FileMode.Create))
            fn.WriteLine("<?xml version=""1.0"" encoding=""utf-8""?>")
            fn.WriteLine("<backuplist>")
            fn.WriteLine("</backuplist>")
            fn.Close() 'all done
        End If
        Dim xd As New XmlDocument
        xd.Load(path)
        Dim root As XmlElement = xd.DocumentElement.SelectSingleNode("/backuplist")
        Dim nodeList As XmlNodeList = root.GetElementsByTagName("backup")
        Dim list As IEnumerator = nodeList.GetEnumerator
        _backupcol = New ObservableCollection(Of BackUpModel)
        _backupcol.Clear()

        Try
            While list.MoveNext
                Dim node As XmlElement = list.Current
                If IO.File.Exists(node.Attributes.GetNamedItem("Path").Value.ToString) Then
                    _backupcol.Add(New BackUpModel(node.Attributes.GetNamedItem("Date").Value.ToString, node.Attributes.GetNamedItem("Path").Value.ToString))
                End If
            End While
        Catch ex As Exception

        End Try
    End Sub

    Public Property Backups As ObservableCollection(Of BackUpModel)
        Get
            Return _backupcol
        End Get
        Set(value As ObservableCollection(Of BackUpModel))
            _backupcol = value
        End Set
    End Property

    Protected Sub NotifyPropertyChanged(info As [String])
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub
    Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class
